package ch.ethz.inf.shop.model;

import java.sql.ResultSet;
import java.sql.SQLException;

import ch.ethz.inf.shop.Title;
import ch.ethz.inf.shop.model.simpleDatabase.Tuple;

/**
 * Object that represents a category of items (i.e. Food)
 */
public final class Category {

	@Title(text = "Category Name", isVisible = true)
	private final String name;
	@Title(text = "Category ID", isVisible = false)
	private final int categoryId;

	@Override
	public String toString() {
		return name;
	}

	public Category(String name, int categoryId) {
		super();
		this.name = name;
		this.categoryId = categoryId;
	}

	public Category(final ResultSet rs) throws SQLException {
		this.categoryId = rs.getInt("category_id");
		this.name = rs.getString("category_name");
	}

	public Category(final Tuple tuple) {
		this.categoryId = tuple.getInt("category.category_id");
		this.name = tuple.getString("category.name");
	}

	public final String getName() {
		return name;
	}

	public int getCategoryId() {
		return categoryId;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + categoryId;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Category other = (Category) obj;
		if (categoryId != other.categoryId)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}
